﻿Imports System.Drawing
Imports System.IO

Public Class ImageInformation

    Public Sub New(ByVal anInmageFolderName As String)
        ImageFolderName = anInmageFolderName
    End Sub

    Private _ImageFolderName As String
    Public Property ImageFolderName() As String
        Get
            Dim myFolder As String = _ImageFolderName
            If myFolder.EndsWith("\"c) Then Return myFolder
            Return myFolder & "\"c
        End Get
        Private Set(ByVal value As String)
            _ImageFolderName = value
        End Set
    End Property

    Private _DicitonaryOfImages As Dictionary(Of String, Image)
    Private ReadOnly Property DictionaryOfImages() As Dictionary(Of String, Image)
        Get
            Dim myDic As Dictionary(Of String, Image) = _DicitonaryOfImages
            If myDic Is Nothing Then
                myDic = New Dictionary(Of String, Image)
                For Each myFile As String In Directory.GetFiles(ImageFolderName)
                    Dim myFI As New FileInfo(myFile)
                    myDic.Add(myFI.Name, New Bitmap(myFI.FullName))
                Next
                _DicitonaryOfImages = myDic
            End If
            Return myDic
        End Get
    End Property

    Public Function GetImage(ByVal anImageName As String) As Image
        If Trim(anImageName).Length = 0 Then Throw New ArgumentNullException
        If Not (DictionaryOfImages.ContainsKey(anImageName)) Then Return My.Resources.unknown
        Dim myImage As Image = DictionaryOfImages.Item(anImageName)
        If Not myImage Is Nothing Then Return myImage
        'return default image
        Return My.Resources.unknown
    End Function

    Public Function GetImage(ByVal anImageName As String, ByVal aSize As Size)
        'Dim TempBitmap As New Bitmap(32, 32)

        'Dim myGraphics As Bitmap
        'myGraphics = New Bitmap(GetImage(anImageName))

        'TempBitmap.Dispose()
        'TempBitmap = Nothing
        'TempBitmap = New Bitmap(aSize.Width, aSize.Height)

        'Dim tempGraphics As Graphics = Graphics.FromImage(TempBitmap)
        'tempGraphics.DrawImage(myGraphics, 0, 0, New Rectangle(0, 0, aSize.Width, aSize.Height), GraphicsUnit.Pixel)

        'Dim myImage As New Bitmap(aSize.Width, aSize.Height, tempGraphics)

        'tempGraphics.Dispose()
        'myGraphics.Dispose()

        'Return myImage


        ' Get the scale factor.
 
        ' Get the source bitmap.
        Dim bm_source As New Bitmap(GetImage(anImageName))

        ' Make a bitmap for the result.
        Dim bm_dest As New Bitmap(aSize.Width, aSize.Height)

        ' Make a Graphics object for the result Bitmap.
        Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

        ' Copy the source image into the destination bitmap.
        gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)

        ' Display the result.
        Return bm_dest

    End Function
End Class
